Skip to content

Condition classes #356

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed

Conversation

chris-pardy
Copy link
Collaborator

A major refactoring of the code around rules / conditions that makes no change to the actual execution of the code but does have a large impact on the potential to extend the rules engine.

Previously the Condition class was used for all the different levels of nested conditions. However only the comparison conditions could be executed with the all, any, not, and condition types being handled by the Rule class. In an effort to make this more extensible I've split the code into 5 condition subclasses for each type of condition and moved the handling code into the condition, out of the Rule. Additionally rather than the Condition constructor knowing how to build a condition I've added a new ConditionConstructor class that knows how to build conditions and passes itself to the Condition subclasses.
With the ConditionConstructor present I've added support for passing it to the Rule and Engine class and then moved the check for top-level conditions to a TopLevelConditionConstructor class. By default there is no change to behavior of the engine, however you can easily set the conditionConstructor property on the Engine to do things like drop the need for top-level conditions, or add new types of conditions. New types of conditions in particular allow for vastly new features to be added that are effectively impossible to support now.

@CacheControl
Copy link
Owner

Will look at this today

Copy link
Owner

@CacheControl CacheControl left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@chris-pardy thanks, the state of the Condition class has been bothering me for a while, so if nothing else this is a nice refactor. Look forward to seeing your longer term ideas come to fruition.

I've got a lot going on in my personal life atm, so I didn't go over this with a fine comb; maybe a good idea to have someone else from your org give it a review.

AllCondition,
AnyCondition,
ConditionConstructor,
neverCondition,
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

nit: Why's the casing different here?

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

neverCondition is a constant the others are classes. more a style question then anything else.

@chris-pardy
Copy link
Collaborator Author

@chris-pardy thanks, the state of the Condition class has been bothering me for a while, so if nothing else this is a nice refactor. Look forward to seeing your longer term ideas come to fruition.

I've got a lot going on in my personal life atm, so I didn't go over this with a fine comb; maybe a good idea to have someone else from your org give it a review.

Sounds good. I'll get a second set of eyes on it. If you think the approach at a high level is in the right direction I'll fix the build issues.

This brings getting the priority of a fact into line with getting the
value of a fact.
Christopher Pardy added 3 commits October 19, 2023 16:08
Split conditions into classes that can be independently evaluated. This
will allow new condition classes to be added by changing
the implementation of the condition constructor.
Support passing a condition constructor into the engine
and the rules in order to allow for custom conditions.
Move the enforcement of top-level conditions in the rules
and the engine to the top-level condition constructor
this also allows for passing Condition instances
to the setCondition method by default

Ultimately this adds a huge amount of support for new
types of conditions that can be plugged into the system.
@chris-pardy
Copy link
Collaborator Author

@CacheControl After doing a bit of thinking I'm actually going to close this PR.
The problem with creating extensible condition classes like this is that they fundamentally break some of the typing that is exposed by the library.

Going into a 7.0 version I think this is the right approach but it's also changes pretty widely what can be done with something like the rule result. For instance if someone added a new "ternary operator condition" to the set of conditions then the structure of the rule result would need to account for this.

@CacheControl
Copy link
Owner

Makes sense, thanks for doing due diligence before proceeding!

@chris-pardy chris-pardy deleted the condition-classes branch October 27, 2023 19:15
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants